//
//  AssistantViewController.m
//  ProductB
//
//  Created by lanou on 16/5/27.
//  Copyright © 2016年 ChenZheng. All rights reserved.
//

#import "AssistantViewController.h"
#import "TravelNoteViewCell.h"
#import "trabelnoteModel.h"
#import "MJRefresh.h"
#import "WriteNoteController.h"
@interface AssistantViewController ()<UITableViewDelegate,UITableViewDataSource,MBProgressHUDDelegate>
@property (nonatomic, strong)UITableView *tableView;

@property (nonatomic, strong)NSMutableArray *dataArray;

@property (nonatomic, strong)NSMutableArray *contentArray;

@property (nonatomic, assign)NSInteger index;

@property (nonatomic,strong ) MBProgressHUD  *hud;
@end

@implementation AssistantViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
    self.hud.labelText = @"正在刷新";
    self.hud.dimBackground = YES;
    self.hud.delegate = self;
    self.index = 1;
    [self requestNetWorkstartURLindex:self.index];
    self.dataArray = [NSMutableArray array];
    self.navigationItem.title = @"游记";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"写游记" style:(UIBarButtonItemStylePlain) target:self action:@selector(writeNote:)];
    [self.navigationItem.rightBarButtonItem setTintColor:[UIColor colorWithRed:0 green:197/255.0 blue:256/255.0 alpha:1]];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerNib:[UINib nibWithNibName:@"TravelNoteViewCell" bundle:nil] forCellReuseIdentifier:@"travelnote"];
    [self.view addSubview:self.tableView];
    //下拉刷新数据
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        //下拉属性数据 需要重新网络请求
        self.index = 1;
        [self requestNetWorkstartURLindex:self.index];
        //停止菊花
        //[self.listTableView.mj_header endRefreshing];
    }];
    //上拉加载更多数据
    self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        //index + 10 之后 重新赋值字典里start对应value
        self.index += 1;
        
        //请求数据
        [self requestNetWorkstartURLindex:self.index];
        [self.tableView.mj_footer endRefreshing];
        
    }];
    

  
}

#pragma mark --- 写游记触发方法
- (void)writeNote:(UIButton *)sender
{
    //判断用户是否登录
    if (0) {
        
    }
    else
    {
        WriteNoteController *writeVc = [[WriteNoteController alloc]init];
        [self.navigationController pushViewController:writeVc animated:YES];
    }
}


#pragma mark ---网络请求---
- (void)requestNetWorkstartURLindex:(NSInteger)index
{
    NSString *string =[NSString stringWithFormat:@"http://q.chanyouji.com/api/v1/timelines.json?interests=&page=%ld&per=50",(long)index];
 [RequestManager initWithUrlString:string requestType:@"GET" parDic:nil finishBlock:^(NSData *data) {
     if (_index == 1) {
         [self.dataArray removeAllObjects];
     }
     NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
     NSArray *activity = dataDic[@"data"];
     for (NSDictionary *dic in activity) {
         trabelnoteModel *model = [[trabelnoteModel alloc ]init];
         NSDictionary *activity = dic[@"activity"];
         model.contentArray = activity[@"contents"];
         model.newdescription = activity[@"description"];
         model.name = activity[@"user"][@"name"];
         model.photo = activity[@"user"][@"photo_url"];
         model.gender = [NSString stringWithFormat:@"%@",activity[@"user"][@"gender"]];
         model.likes_count = [NSString stringWithFormat:@"%@",activity[@"likes_count"]];
         model.favorites_count = [NSString stringWithFormat:@"%@",activity[@"favorites_count"]];
         model.comments_count = [NSString stringWithFormat:@"%@",activity[@"comments_count"]];
         model.topic = activity[@"topic"];
         model.fromname = dic[@"user"][@"name"];
         [self.dataArray addObject:model];
         dispatch_async(dispatch_get_main_queue(), ^{
             [self.tableView reloadData];
             [self.tableView.mj_header endRefreshing];
             [self.hud removeFromSuperview];
         });
     }
     
     
 } errorBlock:^(NSError *error) {
     
 }];

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TravelNoteViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"travelnote" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.dataArray[indexPath.row];
    return cell;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:15] forKey:NSFontAttributeName];
    trabelnoteModel *model = self.dataArray[indexPath.row];
    CGRect bounds = [model.newdescription boundingRectWithSize:CGSizeMake(320, 1000) options:NSStringDrawingUsesLineFragmentOrigin  attributes:dic context:nil];
    return bounds.size.height + 45 + 190 + 90 + 30 + 120 ;
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
